package com.neusoft.nepa.controller;

import javafx.animation.AnimationTimer;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;

public class BackgroundPane extends StackPane {
    private static final double COLOR_CHANGE_SPEED = 0.002;//颜色速度控制
    private static final double ANGLE_CHANGE_SPEED = 0.2;//角度速度控制
    private double hue1 = 0;
    private double hue2 = 180;
    private double angle = 0;
    private AnimationTimer timer;

    public BackgroundPane() {
        // 初始化动画
        timer = new AnimationTimer() {
            private long lastUpdate = 0;
            @Override
            public void handle(long now) {
                if (now - lastUpdate >= 16_666_666) { // 60fps
                    updateBackground();
                    lastUpdate = now;
                }
            }
        };
        timer.start();
    }

    private void updateBackground() {
        hue1 = (hue1 + COLOR_CHANGE_SPEED * 360) % 360;
        hue2 = (hue2 + COLOR_CHANGE_SPEED * 360 * 0.7) % 360;
        angle = (angle + ANGLE_CHANGE_SPEED) % 360;

        Color color1 = Color.hsb(hue1, 0.35, 0.95);
        Color color2 = Color.hsb(hue2, 0.35, 0.95);
        double angleRadians = Math.toRadians(angle);
        double startX = 0.5 + 0.5 * Math.cos(angleRadians);
        double startY = 0.5 + 0.5 * Math.sin(angleRadians);
        double endX = 0.5 - 0.5 * Math.cos(angleRadians);
        double endY = 0.5 - 0.5 * Math.sin(angleRadians);

        LinearGradient gradient = new LinearGradient(
                startX, startY, endX, endY,
                true, javafx.scene.paint.CycleMethod.NO_CYCLE,
                new Stop(0, color1),
                new Stop(1, color2)
        );

        setBackground(new javafx.scene.layout.Background(
                new javafx.scene.layout.BackgroundFill(gradient, null, null)
        ));
    }

    // 停止动画（可选，用于场景销毁时释放资源）
    public void stopAnimation() {
        timer.stop();
    }
}
//